<template>
  <div class="mod-config">
    <el-button-group style="margin-bottom: 10px;">
      <el-button icon="el-icon-search" type="primary" @click="searchHandle">查询</el-button>
      <el-button icon="el-icon-delete" type="primary" @click="clearSearchForm">清空</el-button>
      <el-button v-if="userInfo.roleType&&userInfo.roleType.includes(',ROLE_ADMIN,')" type="primary" @click="syncClassInfoHandle()">同步</el-button>
      <el-button v-if="permissions.admin_pvmclass_add &&userInfo.roleType&&(userInfo.roleType.includes(',ARC_MANAGER,') ||userInfo.roleType.includes(',ROLE_ADMIN,'))" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle('','add')">新增</el-button>
    </el-button-group>
    <el-form inline>
      <el-form-item label="学校">
        <el-select filterable default-first-option clearable v-model="dataForm.schoolId" @change="getGradeOption">
          <el-option v-for="item in schoolOptions" :value="item.id" :label="item.schoolName" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年级">
        <el-select filterable default-first-option clearable v-model="dataForm.gradeId" @change="getClassOption">
          <el-option v-for="item in gradeOptions" :value="item.id" :label="item.gradeName" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" height="calc(100vh - 215px)" border stripe v-loading="dataListLoading">
      <el-table-column prop="sortNum" label="班级序号"></el-table-column>
      <el-table-column prop="className" label="班级名称"></el-table-column>
      <el-table-column prop="gradeName" label="年级名称"></el-table-column>
      <el-table-column prop="schoolName" label="学校名称"></el-table-column>
      <el-table-column prop="memo" label="备注" min-width="120"></el-table-column>
      <el-table-column label="操作" width="230" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-view" @click="addOrUpdateHandle(scope.row.id,'check')">详情</el-button>
          <el-button v-if="permissions.admin_pvmclass_edit &&userInfo.roleType&&(userInfo.roleType.includes(',ARC_MANAGER,') ||userInfo.roleType.includes(',ROLE_ADMIN,'))" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id,'edit')">编辑</el-button>
          <el-button v-if="permissions.admin_pvmclass_del &&userInfo.roleType&&(userInfo.roleType.includes(',ARC_MANAGER,') ||userInfo.roleType.includes(',ROLE_ADMIN,'))" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="avue-crud__pagination" style="margin-bottom: 50px;">
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100, 200]"
        :page-size="pageSize"
        :total="totalPage"
        background
        layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
  </div>
</template>

<script>
  import { filterParams } from '@/util/tools'
  import { remote } from '@/api/admin/dict'
  import { getSchoolList , getClassOptions, getSchoolListBySchoolType } from '@/api/pvm/school'
  import { getGradeBySchoolId } from '@/api/pvm/grade'
  import { fetchList, getObj, addObj, putObj, delObj,syncClassInfo } from '@/api/pvm/class'

  import { mapGetters } from 'vuex'

  import TableForm from './TableForm'

  export default {
    name: 'pvmteacher',
    data() {
      return {
        searchFormShow: true,
        dataList: [],
        dataListLoading: false,
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataForm: {},
        addOrUpdateVisible: false,
        typeOptions: [
          { label: '幼儿园', value: 0 },
          { label: '小学', value: 1 },
          { label: '初中', value: 2 },
          { label: '高中', value: 3 },
          { label: '大学', value: 4 },
        ],
        schoolOptions: [],
        gradeOptions: [],
      }
    },
    components: {
      TableForm
    },
    computed: {
      ...mapGetters(['permissions','userInfo'])
    },
    created() {
      remote('school_type').then(response => {
        let responseData = response.data.data
        let list = []
        responseData.forEach(item => {
          list.push({label: item.label, value: item.value*1})
        })
        this.typeOptions = [...list]
      })
      this.searchHandle()
      this.getSchoolOptions()
    },
    methods: {
      // 查询数据
      searchHandle(){
        this.pageIndex = 1
        this.getDataList()
      },
      // 清空查询表单
      clearSearchForm(){
        for(let key in this.dataForm){
          this.dataForm[key] = ''
        }
      },
      // 获取数据列表
      getDataList(){
        let params1 = {current: this.pageIndex,size: this.pageSize}
        let form = {...this.dataForm}
        let params2 = filterParams(form)
        this.dataListLoading = true
        fetchList(Object.assign(params1,params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.mdg)
          }
          this.dataListLoading = false
        })
      },
      // 获取学校列表
      getSchoolOptions(){
        let schoolType = ''
        getSchoolListBySchoolType(schoolType).then(response => {
          if(response.data.code == '0'){
            this.schoolOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 获取年级列表
      getGradeOption(schoolId){
        this.gradeOptions = []
        this.$set(this.dataForm,'gradeId','')
        getGradeBySchoolId(schoolId).then(response => {
          if(response.data.code == '0'){
            this.gradeOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 查询年级列表
      getGradeList(){
        fetchGradeList({current: 0,size: 100}).then(response => {
          if(response.data.code == '0'){
            this.gradeOptions = response.data.data
          }else{
            this.$message.error(response.data.mdg)
          }
        })
      },
      // 每页条数改变
      sizeChangeHandle(val){
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页改变
      currentChangeHandle(val){
        this.pageIndex = val
        this.getDataList()
      },
      // 打开弹窗 新增 / 修改 / 详情
      addOrUpdateHandle(id,type){
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id,type)
        })

      },
      syncClassInfoHandle(){
          let form = {...this.dataForm}
          let params2 = filterParams(form)
          syncClassInfo(params2).then(response=>{
            if(response.data.code == '0'){
              this.$message.success('同步成功')
              this.getDataList()
            }else{
              this.$message.error(response.data.mdg)
            }
          })
      },
      // 删除班级
      deleteHandle(row){
        this.$confirm('此操作将永久删除该班级(班级名为:' + row.teacherName + '), 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delObj(row.id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.mdg)
          }
        })
      },
      //  转换类型格式
      statusFormat(val){
        let obj = {}
        obj = this.typeOptions.find(item => {
          if(item.value == val){
            return item
          }
        })
        if(obj && obj.label){
          return obj.label
        }
      }
    }
  }
</script>
